import { DataLoader } from "loader/DataLoader";
import { JsonMergerConst } from "../utils/JsonMergerConst";
import ByteArray = require("bytearray-node")
//class ConfigManager
    
    export  class ConfigManager /* flash.events.EventDispatcher */
    {
        constructor()
        {
            
            if (ConfigManager._instance != null) 
            {
                throw new Error("ConfigManager 单例");
            }
            this._isLoaded = false;
            return;
        }

        public load(arg1: string): void
        {
            var loc1=new DataLoader();
            loc1.addEventListener(flash.events.Event.COMPLETE, this.onLoadCompleteHandler);
            loc1.addEventListener(flash.events.ProgressEvent.PROGRESS, this.onLoadProgressHandler);
            loc1.addEventListener(flash.events.ErrorEvent.ERROR, this.onErrorHandler);
            loc1.load(arg1, null);
            return;
        }

        /* internal  */onLoadCompleteHandler(arg1: Object /* flash.events.Event */): void
        {
            var event: Object /* flash.events.Event */;
            var loader: DataLoader;
            var byteArray: ByteArray;
            var tim: number;

            var loc1;
            loader = null;
            byteArray = null;
            tim = NaN;
            event = arg1;
            try 
            {
                this._isLoaded = true;
                loader = event.target as DataLoader;
                byteArray = loader.bytesData;
                byteArray.position = 0;
                tim = flash.utils.getTimer();
                byteArray.uncompress();
                tim = flash.utils.getTimer();
                this._configDic = byteArray.readObject() as Map<any, any> /* flash.utils.Dictionary */;
                dispatchEvent(event);
            }
            catch (e)
            {
                _isLoaded = false;
                _configDic = null;
                dispatchEvent(new Object /* flash.events.Event */(flash.events.IOErrorEvent.IO_ERROR));
            }
            return;
        }

        /* internal  */onLoadProgressHandler(arg1: Object /* flash.events.ProgressEvent */): void
        {
            dispatchEvent(arg1);
            return;
        }

        /* internal  */onErrorHandler(arg1: Object /* flash.events.ErrorEvent */): void
        {
            this._isLoaded = false;
            dispatchEvent(arg1);
            return;
        }

        public getObjectByFileName(arg1: string): Object
        {
            var loc1=null;
            if (this.hasFile(arg1)) 
            {
                loc1 = this._configDic[arg1].content as ByteArray;
                loc1.position = 0;
                return loc1.readObject().root;
            }
            return null;
        }

        public getStringByFileName(arg1: string): string
        {
            if (this.hasFile(arg1)) 
            {
                return this._configDic[arg1].content;
            }
            return null;
        }

        public getJSONByFileName(arg1: string): Object
        {
            var loc1=null;
            var loc2=null;
            if (this.hasFile(arg1)) 
            {
                loc1 = this._configDic[arg1].content as ByteArray;
                loc1.position = 0;
                loc2 = loc1.readObject();
                loc2 = JsonMergerConst.instance.revertAttributes(arg1, loc2);
                return loc2;
            }
            return null;
        }

        public hasFile(arg1: string): Boolean
        {
            if (this._isLoaded == false) 
            {
                return false;
            }
            return !(this._configDic[arg1] == null);
        }

        public getByteArrayByFileName(arg1: string): ByteArray
        {
            if (this.hasFile(arg1)) 
            {
                return this._configDic[arg1].content as ByteArray;
            }
            return null;
        }

        public static get instance(): ConfigManager
        {
            if (ConfigManager._instance == null) 
            {
                ConfigManager._instance = new ConfigManager();
            }
            return ConfigManager._instance;
        }

        private /* var */_isLoaded: Boolean=false;

        private /* var */_configDic: Map<any, any> /* flash.utils.Dictionary */;

        private static /* var */_instance: ConfigManager;
    }
